﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace DentistOffice
{
    public partial class Faktury : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(1000);
            DentistEntities DE = new DentistEntities();
            MemoryStream stream = new MemoryStream();
            int id = 0, vid = 0, sesja = 0;
            try
            {
                id = Int16.Parse(Request.QueryString["id"]);
                sesja = Int16.Parse(Session["isLogged"].ToString());
                vid = Int16.Parse(Request.QueryString["vid"]);
            }
            catch (Exception)
            { }
            if (sesja != id)
            {
                Response.Write("Możesz przeglądać tylko swoje faktury!");
            }
            else
            {
                var query = from p in DE.Patients where p.id == id select p;
                var query2 = from p in DE.VisitsHistories where p.id == vid select p;
                var pacjent = query.FirstOrDefault();
                var his = query2.FirstOrDefault();
                var chk = from p in DE.FHis where(p.idPac == id && p.idVis == vid) select p;
                 FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts");
                    Font sb = FontFactory.GetFont("Arial", BaseFont.CP1250, true, 8, Font.BOLD);
                    Font s = FontFactory.GetFont("Arial", BaseFont.CP1250, true, 8);
                    Font n = FontFactory.GetFont("Arial", BaseFont.CP1250, true, 11);
                    Font d = FontFactory.GetFont("Arial", BaseFont.CP1250, true, 16);
                PdfPCell cell = null;
                if (chk.Count() == 0)
                {
                    FHi fhis = new FHi();
                    fhis.idVis = vid;
                    fhis.idPac = id;
                    fhis.data = DateTime.Now;
                    fhis.zaplacona = 1;
                    DE.AddToFHis(fhis);
                    DE.SaveChanges();
                    cell = new PdfPCell(new Phrase("Faktura VAT nr " + fhis.id + "/" + DateTime.Now.Year, d));
                }
                string fileName;
                do
                {
                    fileName = Guid.NewGuid().ToString() + ".pdf";
                } while (System.IO.File.Exists(fileName));
                Document doc = new Document(iTextSharp.text.PageSize.A4);
                try
                {
                    //PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "\\" + fileName, FileMode.Create));
                    PdfWriter wri = PdfWriter.GetInstance(doc, stream);
                    doc.Open();
                    PdfPTable table = new PdfPTable(1);
                    if (chk.Count() != 0)
                    {
                        var idh = chk.FirstOrDefault();
                        cell = new PdfPCell(new Phrase("Faktura VAT nr " + idh.id + "/" + DateTime.Now.Year, d));
                    }
                    Phrase p1 = new Phrase();
                    cell.PaddingBottom = 15f;
                    cell.PaddingTop = 10f;
                    cell.Colspan = 3;
                    cell.BackgroundColor = BaseColor.LIGHT_GRAY;
                    cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                    table.AddCell(cell);
                    doc.Add(table);
                    doc.Add(new Paragraph("\n"));
                    table = new PdfPTable(2);
                    p1.Add(new Chunk("Data wystawienia: ", s));
                    string dzisiaj = DateTime.Today.ToString();
                    string termin = (DateTime.Today.AddDays(7)).ToString();
                    dzisiaj = dzisiaj.Substring(0, 10);
                    p1.Add(new Chunk(dzisiaj.Substring(0, 10), sb));
                    cell = new PdfPCell(p1);
                    cell.HorizontalAlignment = 1;
                    table.AddCell(cell);
                    Phrase p2 = new Phrase();
                    p2.Add(new Chunk("Data zakupu: ", s));
                    p2.Add(new Chunk((his.visitDate.ToString()).Substring(0, 10), sb));
                    cell = new PdfPCell(p2);
                    cell.HorizontalAlignment = 1;
                    table.AddCell(cell);
                    doc.Add(table);
                    doc.Add(new Paragraph("\n"));
                    table = new PdfPTable(2);
                    p1.Clear();
                    p1.Add(new Chunk("Sprzedawca: ", s));
                    p1.Add(new Chunk("Gabinet Dentystyczny Adam Smith", sb));
                    p1.Add(new Chunk("\n\nAdres: ", s));
                    p1.Add(new Chunk("ul. Żołnierska 49, 70-374 Szczecin", sb));
                    p1.Add(new Chunk("\n\nNIP: ", s));
                    p1.Add(new Chunk("734-152-64-25", sb));
                    cell = new PdfPCell(p1);
                    table.AddCell(cell);
                    p2.Clear();
                    p2.Add(new Chunk("Nabywca: ", s));
                    p2.Add(new Chunk(pacjent.companyName + " " + pacjent.name + " " + pacjent.surname, sb));
                    p2.Add(new Chunk("\n\nAdres: ", s));
                    p2.Add(new Chunk("ul. " + pacjent.street + ", " + (pacjent.cityCode.ToString()).Substring(0, 2) + "-" + (pacjent.cityCode.ToString()).Substring(2) + " " + pacjent.city, sb));
                    p2.Add(new Chunk("\n\nNIP: ", s));
                    p2.Add(new Chunk((pacjent.nip.ToString()).Substring(0, 3) + "-" + (pacjent.nip.ToString()).Substring(3, 3) + "-" + (pacjent.nip.ToString()).Substring(6, 2) + "-" + (pacjent.nip.ToString()).Substring(8, 2), sb));
                    cell = new PdfPCell(p2);
                    table.AddCell(cell);
                    doc.Add(table);
                    doc.Add(new Paragraph("\n"));
                    table = new PdfPTable(1);
                    p1.Clear();
                    p1.Add(new Chunk("Forma płatności: ", s));
                    p1.Add(new Chunk("przelew", sb));
                    p1.Add(new Chunk("      Termin płatności: ", s));
                    p1.Add(new Chunk(termin.Substring(0, 10), sb));
                    p1.Add(new Chunk("\n\nBank: ", s));
                    p1.Add(new Chunk("TheBank Sp. z o.o. ", sb));
                    p1.Add(new Chunk("     Konto: ", s));
                    p1.Add(new Chunk("64 1121 0000 1516 4116 1515 1214", sb));
                    cell = new PdfPCell(p1);
                    table.AddCell(cell);
                    doc.Add(table);
                    doc.Add(new Paragraph("\n"));
                    table = new PdfPTable(5);
                    cell = new PdfPCell(new Paragraph("Nazwa", sb));
                    cell.BackgroundColor = BaseColor.LIGHT_GRAY;
                    table.AddCell(cell);
                    cell = new PdfPCell(new Paragraph("Wartość netto", sb));
                    cell.BackgroundColor = BaseColor.LIGHT_GRAY;
                    table.AddCell(cell);
                    cell = new PdfPCell(new Paragraph("Stawka VAT", sb));
                    cell.BackgroundColor = BaseColor.LIGHT_GRAY;
                    table.AddCell(cell);
                    cell = new PdfPCell(new Paragraph("Kwota VAT", sb));
                    cell.BackgroundColor = BaseColor.LIGHT_GRAY;
                    table.AddCell(cell);
                    cell = new PdfPCell(new Paragraph("Wartość brutto", sb));
                    cell.BackgroundColor = BaseColor.LIGHT_GRAY;
                    table.AddCell(cell);
                    doc.Add(table);
                    table = new PdfPTable(5);
                    double kwota = (float)his.koszt;
                    double vat = kwota * 0.23;
                    double calosc = kwota + vat;
                    cell = new PdfPCell(new Paragraph("Usługi dentystyczne", s));
                    table.AddCell(cell);
                    cell = new PdfPCell(new Paragraph(kwota.ToString(), s));
                    table.AddCell(cell);
                    cell = new PdfPCell(new Paragraph("23%", s));
                    table.AddCell(cell);
                    cell = new PdfPCell(new Paragraph(vat.ToString(), s));
                    table.AddCell(cell);
                    cell = new PdfPCell(new Paragraph(calosc.ToString(), s));
                    table.AddCell(cell);
                    doc.Add(table);
                    doc.Add(new Paragraph("\n"));
                    table = new PdfPTable(1);
                    p1.Clear();
                    p1.Add(new Chunk("\nRazem do zapłaty: ", s));
                    p1.Add(new Chunk(calosc.ToString() + " PLN\n\n", sb));
                    cell = new PdfPCell(p1);
                    table.AddCell(cell);
                    doc.Add(table);
                }
                catch (Exception) { }
                finally
                {
                    doc.Close();
                    /*Response.ContentType = ContentType;
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
                    Response.WriteFile(Server.MapPath("~/" + fileName));
                    Response.Flush();
                    System.IO.File.Delete(Server.MapPath("~/" + fileName));
                    Response.End();*/
                    Response.ContentType = "application/pdf";
                    Response.Clear();
                    Response.OutputStream.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
                    Response.OutputStream.Flush();
                    stream.Close();
                    Response.End();
                }
            }
        }
    }
}